package cc.eddic.examinationsystem.dto;

import cc.eddic.examinationsystem.domain.Exam;
import lombok.*;
import org.springframework.beans.BeanUtils;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ExamModel {
    private Long id;
    @NotEmpty(message = "名称不能为空")
    private String name;
    private String examAt;
    private Integer timeLimit;
    @Min(value = 1, message = "总分不能小于 1")
    @Max(value = 200, message = "总分不能大于 200")
    private Integer totalScore;
    private Integer version;
    public static ExamModel from(final Exam exam){
        val dto=new ExamModel();
        BeanUtils.copyProperties(exam,dto);

        //获取数据库的时间“2022-02-09 10:21”，转化为前端需要的字符串格式的时间“2022-02-09T10:21”
        dto.setExamAt( df.format(exam.getExamAt()));
        return dto;
    }

   private static final DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
}
